(display "\n========================================\n")
(define (accmulate op initial seq)
    (if (null? seq)
        initial
        (op (car seq)
            (accmulate op initial (cdr seq)))))

(define (enumerate-interval low high)
    (if (> low high)
        ()
        (cons low (enumerate-interval (+ low 1) high))))

(define (flatmap proc seq)
    (accmulate append () (map proc seq)))

(define (unique-pairs n)
    (flatmap (lambda (i)
                     (map (lambda (j) (list i j))
                          (enumerate-interval 1 (- i 1))))
             (enumerate-interval 1 n)))
; (display (unique-pairs 6))
; (newline)

(define (unique-triples n)
    (flatmap (lambda (i)
                     (map (lambda (j)
                                  (cons i j))
                          (unique-pairs (- i 1))))
             (enumerate-interval 1 n)))

(define ut (unique-triples 5))
(display ut)
(newline)

(define (three-sum n s)
    (filter (lambda (uti)
                    (= s (+ (car uti) (cadr uti) (caddr uti))))
            (unique-triples n)))

(display (three-sum 5 10))

(display "\n========================================\n")
